package com.cqggrw.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.cqggrw.dao.TouPiaoDaoI;
import com.cqggrw.domain.TPiao;
import com.cqggrw.domain.TPiaoOpt;
import com.cqggrw.util.DBUtil;
import com.cqggrw.util.Utils;

public class TouPiaoDaoImpl implements TouPiaoDaoI {

	public boolean add(String id, String name) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_toupiao(id, name, createDate) values(?, ?, now());";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			ps.setString(2, name);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}
	public boolean addOpt(String objId, String name) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "insert into tbl_toupiaoopt(name, objId) values(?, ?);";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, objId);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}

	public boolean del(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_toupiaoopt where id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}


	public TPiao getOne(TPiao t) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "SELECT * FROM tbl_toupiao where id = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, t.getId());
			rs = ps.executeQuery();
			if (rs.next()) {
				t.setName(rs.getString("name"));
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return t;
	}

	public List<TPiao> get() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<TPiao> list = new ArrayList<TPiao>();
		TPiao tp = null;
		
		String sql = "select * from tbl_toupiao ORDER BY createDate;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			while (rs.next()) {
				tp = new TPiao();
				tp.setId(rs.getString("id"));
				tp.setName(rs.getString("name"));
				list.add(tp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}
	public int getCount() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		int total = 0;
		
		String sql = "select count(*) from tbl_toupiao;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			if (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return total;
	}
	public List<TPiaoOpt> getOptList(String id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<TPiaoOpt> list = new ArrayList<TPiaoOpt>();
		TPiaoOpt tp = null;
		
		String sql = "select * from tbl_toupiaoopt where objId = ? ;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();

			while (rs.next()) {
				tp = new TPiaoOpt();
				tp.setId(rs.getInt("id"));
				tp.setName(rs.getString("name"));
				tp.setCount(rs.getInt("count"));
				list.add(tp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return list;
	}
	public boolean modifyOpt(Integer id, String name) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "update tbl_toupiaoopt set name = ? WHERE ID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setInt(2, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}
	public boolean modify(TPiao tp) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "update tbl_toupiao set name = ? WHERE ID = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, tp.getName());
			ps.setString(2, tp.getId());
			i = ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}
	public boolean delObj(String id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "delete from tbl_toupiao where id = ?;";
		int i = 0;
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			i = ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return i == 1;
	}
	public String getLastId() {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String id = null;
		
		String sql = "SELECT id FROM tbl_toupiao ORDER BY createDate DESC LIMIT 1;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();

			if (rs.next()) {
				id = rs.getString(1);
			}
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
		return id;
	}
	public void doToupiao(Integer id) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "update tbl_toupiaoopt set count = count+1 WHERE ID = ?;";
		try {
			conn = DBUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, id);
			ps.executeUpdate();
		} catch (Exception e) {
			Utils.log(e);
		} finally {
			DBUtil.close(conn, ps, rs);
		}
	}

}
